package com.async.controller;

import com.async.Hander.SyncExctionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;


@Controller
public class 多线程排除异常线程_Controller {

    @Autowired
    private SyncExctionHandler syncExctionHandler;

    @Autowired
    private PlatformTransactionManager transactionManager;


    @Autowired
    @Qualifier("test1")
    private ThreadPoolTaskExecutor taskExecutor;

    @ResponseBody
    @RequestMapping("/test4")
    public String test4() {
        boolean flag = false;


        return "test4";
    }

    @ResponseBody
    @RequestMapping("/test2")
    public String test2() throws InterruptedException {
        CountDownLatch threadLatchs = new CountDownLatch(3);
        System.out.println("请求接口开始----->" + LocalDateTime.now());
        CompletableFuture<Void> task1 = CompletableFuture.runAsync(() -> {
            try {

//                transactionalUntil.wait();
                threadLatchs.countDown();
                Thread.sleep(5000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("task1 线程name-->" + Thread.currentThread().getName());
        }, taskExecutor);
        CompletableFuture<Void> task3 = CompletableFuture.runAsync(() -> {
            threadLatchs.countDown();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("task3 线程name-->" + Thread.currentThread().getName());
        }, taskExecutor);
        CompletableFuture<Void> task2 = CompletableFuture.runAsync(() -> {
            threadLatchs.countDown();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("task2 线程name-->" + Thread.currentThread().getName());
        }, taskExecutor);

        threadLatchs.await();
        try {
            CompletableFuture.allOf(task1, task2, task3).get();
            System.out.println("请求接口结束----->" + LocalDateTime.now());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "ok";
    }


  /*  @RequestMapping("/test3")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public String test3() throws Exception {
//        transactionalUntil.begin();
        List<Future<Boolean>> futureList = new ArrayList<>();
        List<TransactionStatus> statusList=new ArrayList<TransactionStatus>();
        statusList= Collections.synchronizedList(statusList);

        for (int i = 0; i < 5; i++) {
            futureList.add(syncExctionHandler.syncException1(i, transactionManager,statusList));
        }
        for (Future<Boolean>   future : futureList){
            if (null!= future){
                future.get();
                System.out.println("111111111111");
            }
        }

        System.out.println(statusList.size());
        statusList.forEach(i->{
            transactionManager.commit(i);
        });

        System.out.println(statusList.size());
        return "ok";
    }
*/
}
